package io.yuncheng.modules.job.task;

import com.alibaba.fastjson.JSON;
import io.yuncheng.common.config.DictConfig;
import io.yuncheng.common.utils.Constant;
import io.yuncheng.common.utils.DateUtils;
import io.yuncheng.common.utils.OuterServerUrlConstant;
import io.yuncheng.common.utils.SysDictConstant;
import io.yuncheng.modules.job.entity.ScheduleParamEntity;
import io.yuncheng.modules.sys.entity.SubApplicationEntity;
import io.yuncheng.modules.sys.entity.SubCascadePlatformLogEntity;
import io.yuncheng.modules.sys.entity.SysDictEntity;
import io.yuncheng.modules.sys.service.SubCascadePlatformLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 同步应用数据
 */
@Service
@Slf4j
public class AppTaskImpl extends AppTask {
    @Resource
    SyncApplicationTask syncApplicationTask;
    @Resource
    private SubCascadePlatformLogService subCascadePlatformLogService;
    @Resource
    private DictConfig config;
    @Override
    void exeApplicationSave(Map<String, Object> map, SubApplicationEntity entity, Date exeDate) {
        String deptIdStr = config.getSysDictValue(SysDictConstant.SYS_LEVEL, Constant.DICT_DEFAULT_CODE);
        Long rootDeptId = Long.valueOf(deptIdStr);
        SysDictEntity sysDict = config.getSysDict(SysDictConstant.SYS_LEVEL_TYPE, Constant.DICT_DEFAULT_CODE);
        String server = sysDict.getRemark();
        //TODO 根据开始结束时间执行
        /**
         * 算法日志
         */
        syncApplicationTask.syncArithLog(entity.getAppId(),entity.getDeptId(),rootDeptId, server + OuterServerUrlConstant.ARITH_LOG_SAVE);
        /**
         * 密评
         */
        syncApplicationTask.syncAssessment(entity.getAppId(),entity.getDeptId(),rootDeptId,server + OuterServerUrlConstant.ASSESSMENT_LOG_SAVE);
        /**
         * 告警日志
         */
        syncApplicationTask.syncWarningLog(entity.getUniqueAppId(),entity.getDeptId(),rootDeptId,server + OuterServerUrlConstant.WARNING_LOG_SAVE);
    }
    @Override
    public void run(String params) {
        ScheduleParamEntity entity = JSON.parseObject(params, ScheduleParamEntity.class);
        execute(entity);
    }

    /**
     * 重写任务的时间
     * @param entity
     */
    @Override
    public void exeByDate(ScheduleParamEntity entity){
        List<SubCascadePlatformLogEntity> list = subCascadePlatformLogService.list();
        List<SubCascadePlatformLogEntity> collect = list.stream().filter(a -> Constant.SysLevelEnum.CITY.getValue() == a.getCascadeType()).collect(Collectors.toList());
        //同步当前到一年以前的数据
        Date maxDate = new Date();
        if(!ObjectUtils.isEmpty(collect)){
            collect.stream().map(u -> u.getCascadeTime()).max(Date::compareTo).get();
        }
        entity.setStartDate(DateUtils.getCurrentQuarterDateBegin(maxDate));
        entity.setEndDate(DateUtils.getCurrentQuarterDateEnd(maxDate));
        forEachCollect(entity);
    }
}
